<!--
* @Author: zhangjing
* @Date: 2025-06-17 17:08:31
* @Description: 绑定设备列表=>
-->
<template>
	<el-dialog title="绑定设备" :visible.sync="detailVisible" width="1000px" :before-close="handleClose">
		<el-tabs v-model="bindStatus" @tab-click="handleClick">
			<el-tab-pane label="未绑定" name="0"></el-tab-pane>
			<el-tab-pane label="已绑定" name="1"></el-tab-pane>
		</el-tabs>
		<avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :search.sync="query"
			v-model="form" ref="crud" @search-change="searchChange" @search-reset="searchReset"
			@selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
			@refresh-change="refreshChange" @on-load="onLoad">
			<template #menu="{ size, row }">
				<el-button @click="confirm(row)" :size="size" type="text">{{ row.bindStatus ? '解绑' : '绑定' }}</el-button>
			</template>
		</avue-crud>
	</el-dialog>
</template>

<script>
	import {
		crudMxins
	} from '@/mixins/index';
	import {
		getBindDevice,
		bindDevice,
		unBindDevice
	} from '@/api/ecgManage/patient';
	import {
		removeEmptyValues
	} from '@/utils/index.js'
	export default {
		mixins: [crudMxins],
		data() {
			return {
				detailVisible: false,
				bindStatus: '1'
			};
		},
		computed: {
			option() {
				return {
					index: true,
					indexLabel: '序号',
					calcHeight: 30,
					searchShow: true,
					searchMenuSpan: 5,
					addBtn: false,
					editBtn: false,
					searchLabelWidth: 80,
					dialogClickModal: false,
					dialogDrag: true,
					tip: false,
					border: true,
					menu: true,
					searchIndex: 10,
					searchIcon: true,
					span: 12,
					viewBtn: false,
					delBtn: false,
					menuWidth: 100,
					header: false,
					column: [{
							label: '设备mac',
							prop: 'deviceMac',
							search: true
						},
						{
							label: '设备名称',
							prop: 'deviceName',
							search: true
						},
						{
							label: '设备类型',
							prop: 'deviceType',
							type: 'select',
							search: true,
							// searchMultiple: true,
							dicData: [{
								label: '心电',
								value: 1
							}, {
								label: '血压',
								value: 2
							}]
						}
					]
				};
			}
		},
		methods: {
			handleOpen(hospPatientId) {
				this.hospPatientId = hospPatientId;
				this.detailVisible = true;
			},
			handleClose() {
				this.detailVisible = false;
			},
			rowDel(row, index) {
				console.log(row, index);
				this.$confirm('确定删除本条信息?', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					delData({
						idList: [row.id]
					}).then(res => {
						this.$message({
							type: 'success',
							message: '操作成功!'
						});
						// 数据回调进行刷新
						this.onLoad(this.page);
					});
				});
			},
			onLoad(page, params = {}) {
				this.loading = true;
				let bindStatus = this.bindStatus - 0;
				let deviceTypes = params.deviceType;
				if (deviceTypes) {
					deviceTypes = [deviceTypes];
					delete params.deviceType
				} else {
					deviceTypes = [1, 2]
				}

				console.log('this.query', this.query)
				getBindDevice(
					Object.assign({}, removeEmptyValues(params), {
							pageNum: page.currentPage,
							pageSize: page.pageSize,
							bindStatus,
							deviceType: deviceTypes
						},
						bindStatus ? {
							hospPatientId: this.hospPatientId
						} : {}
					)
				).then(res => {
					this.page.total = res.data.total;
					this.data = res.data.records;
					this.loading = false;
					this.selectionClear();
				});
			},
			handleClick(e) {
				console.log('e', e.name);
				this.bindStatus = e.name;
				this.query = {};
				this.refreshChange();
			},
			confirm(row) {
				this.$confirm(row.bindStatus ? '你确定解绑吗?' : '你确定绑定吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: row.bindStatus ? 'error' : 'success'
				}).then(() => {
					if (row.bindStatus) {
						this.unBindDevice(row);
					} else {
						this.bindDevice(row);
					}
				});
			},
			bindDevice(row) {
				bindDevice({
					idList: [row.id],
					hospPatientId: this.hospPatientId
				}).then(res => {
					if (res.result) {
						this.$modal.msgSuccess('绑定指令下发成功');
						this.refreshChange();
					}
				});
			},
			unBindDevice(row) {
				unBindDevice({
					idList: [row.id],
					hospPatientId: this.hospPatientId
				}).then(res => {
					if (res.result) {
						this.$modal.msgSuccess('解绑指令下发成功');
						this.refreshChange();
					}
				});
			}
		}
	};
</script>

<style></style>